
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;

import java.io.File;  
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.transform.OutputKeys;  
import javax.xml.transform.Transformer;  
import javax.xml.transform.TransformerConfigurationException;  
import javax.xml.transform.TransformerException;  
import javax.xml.transform.TransformerFactory;  
import javax.xml.transform.dom.DOMSource;  
import javax.xml.transform.stream.StreamResult;  
  
import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.NamedNodeMap;  
import org.w3c.dom.Node;  
import org.w3c.dom.NodeList;  
import org.xml.sax.SAXException;

import com.alibaba.fastjson.JSONObject;  

public class xml_dom {  
  
    public static void main(String[] args) {  
    	
    	System.out.println(JSONObject.toJSON(xml_dom.xml_dom_parse()));
    }  
      
      
     public static List xml_dom_parse() {  
           DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 
           List list = new ArrayList<Map>();
            try {  
                DocumentBuilder db=dbf.newDocumentBuilder();  
                Document docu=db.parse("src\\main\\resources\\menuConfig.xml");  
                //获取所有的书籍节点  
                NodeList booklist=docu.getElementsByTagName("ul");  
                for(int i =0;i<booklist.getLength();i++){  
            	Map map = new HashMap<String, String>();
                Node book_item=booklist.item(i);  
                map.put("firstCode", book_item.getAttributes().getNamedItem("firstCode").getNodeValue())  ;     
                map.put("name", book_item.getAttributes().getNamedItem("name").getNodeValue())  ;     
                
                NodeList book_child=book_item.getChildNodes();  
                List list2 = new ArrayList<Map>();
                for(int k=0;k<book_child.getLength();k++){  
                	Map map2 = new HashMap<String, String>();
                    Node book_child_ele=book_child.item(k); 
                    if(book_child_ele.getNodeType()==Node.ELEMENT_NODE){//如果没有会打印出很多空格，因为text也是一种节点类型，  
                    	map2.put("code", book_child_ele.getAttributes().getNamedItem("code").getNodeValue());
                    	map2.put("ids", book_child_ele.getAttributes().getNamedItem("ids").getNodeValue());
                    	map2.put("name", book_child_ele.getAttributes().getNamedItem("name").getNodeValue());
                    	map2.put("url", book_child_ele.getAttributes().getNamedItem("url").getNodeValue());
                    } 
                    if(!map2.isEmpty()){
                    	list2.add(map2);
                    }
                    
                    map.put("sub", list2);
                }  
                list.add(map);
               }  
            } catch (ParserConfigurationException e) {  
                e.printStackTrace();  
            } catch (SAXException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            
            return list;
        }  
       
}  